import Vue from 'vue'
import Router from 'vue-router'
import Home from "../views/Home";
import UserGroup from '../views/pages/system/usergroup'
import User from '../views/pages/system/user'
import Role from '../views/pages/system/role'
import Permission from '../views/pages/system/permission'

import MonitorData from '../views/pages/monitor/data'
import MonitorOnline from '../views/pages/monitor/online'
import MonitorSchedule from '../views/pages/monitor/schedule'
import MonitorServer from '../views/pages/monitor/server'

import Interface from '../views/pages/tool/interface'

import LoginLog from '../views/pages/log/LoginLog'
import OpLog from '../views/pages/log/OpLog'


import Login from "../views/Login";
import Question from '../views/pages/paper-question/question'
import Paper from '../views/pages/paper-question/paper'
import ExamCreate from '../views/pages/exam/create'
import ExamJoin from '../views/pages/exam/join'
import ExamList from '../views/pages/exam/list'
import ExamReport from '../views/pages/exam/report'

import UserInfo from '../views/pages/user'

import store from '../store'
Vue.use(Router);

const router = new Router({
  mode:'history',
  routes: [
    {
      path:'/',
      redirect:'/home'
    },

    {
      path:'/login',
      component:Login
    },




    {
      path: '/home',
      component: Home,
      redirect:'/system/user',
      children:[
        {
          path: '/userinfo',
          component: UserInfo
        },

        {
          path:'/system/group',
          component: UserGroup
        },
        {
          path:'/system/user',
          component: User
        },
        {
          path:'/system/role',
          component: Role
        },
        {
          path:'/system/permission',
          component:Permission
        },
        {
          path:'/monitor/online',
          component:MonitorOnline
        },

        {
          path:'/monitor/data',
          component:MonitorData
        },
        {
          path:'/monitor/schedule',
          component:MonitorSchedule
        },
        {
          path:'/monitor/server',
          component:MonitorServer
        },
        {
          path:'/tool/interface',
          component:Interface
        },

        {
          path: '/log/login',
          component:LoginLog
        },

        {
          path:'/log/op',
          component:OpLog
        },

        {
          path:'/question/create',
          component:Question
        },
        {
          path:'/paper/create',
          component:Paper
        },
        {
          path:'/exam/create',
          component:ExamCreate
        },
        {
          path:'/exam/list',
          component:ExamList
        },
        {
          path:'/exam/report',
          component:ExamReport
        },
        {
          path:'/exam/join',
          component:ExamJoin
        }

      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if(to.path!='/login'){
    if(!store.getters.token){
      next('/login')
    }else{
      next();
    }
  }
  else {
    if(store.getters.token){
      next('/home');
    }else{
      next();
    }
  }

})

export default router;
